import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _flow from "lodash/flow";
import _isUndefined from "lodash/isUndefined";

var _dec, _dec2, _dec3, _class;

/**
 * SearchPage - 查询模板
 * @date: 2018-7-20

 */
// TODO: max-classed-per-file
// eslint-disable-next-line max-classes-per-file
import React, { Component } from 'react';
import { Form, Button } from 'antd';
import { Header, Content } from "../Page";
import EditTable from "../EditTable";
import CustomTable from "../CustomTable";
import { Bind } from 'lodash-decorators';
import { filterNullValueObject } from "../../utils/utils";
import intl from "../../utils/intl";
import cacheComponent from "../CacheComponent";
import BaseFilterForm from "./FilterForm";
/**
 * 通用查询模板.
 *
 * 1. componentDidMount 方法中判断是从其他路由回退进而选择是查询还是刷新
 * 2. 子类需要在 props 中映射 list,loading
 * 3. 子类覆盖部分方法
 *
 *
 * @export
 * @class OptionInput
 * @extends {React.Component}
 */

var SearchPage = (_dec = Bind(), _dec2 = Bind(), _dec3 = Bind(), (_class = /*#__PURE__*/function (_Component) {
  _inherits(SearchPage, _Component);

  var _super = _createSuper(SearchPage);

  function SearchPage(props) {
    var _this;

    _classCallCheck(this, SearchPage);

    _this = _super.call(this, props);

    var pageConfig = _this.pageConfig();

    var SearchBasicForm = /*#__PURE__*/function (_BaseFilterForm) {
      _inherits(SearchBasicForm, _BaseFilterForm);

      var _super2 = _createSuper(SearchBasicForm);

      function SearchBasicForm() {
        _classCallCheck(this, SearchBasicForm);

        return _super2.apply(this, arguments);
      }

      return _createClass(SearchBasicForm);
    }(BaseFilterForm);

    var formWrapper = pageConfig.cacheKey ? _flow(cacheComponent({
      cacheKey: pageConfig.cacheKey
    }), Form.create({
      fieldNameProp: null
    })) : Form.create({
      fieldNameProp: null
    });
    _this.FilterForm = formWrapper(SearchBasicForm);
    _this.pageConfig = pageConfig;
    _this.filterForm = {};
    _this.state = {
      selectedRows: []
    };

    _this.customConstructor(props); // 初始化自定义数据


    return _this;
  }

  _createClass(SearchPage, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var modelName = this.pageConfig.modelName;
      var _this$props = this.props,
          _this$props$modelName = _this$props[modelName],
          searchPageData = _this$props$modelName === void 0 ? {} : _this$props$modelName,
          _this$props$location$ = _this$props.location.state;
      _this$props$location$ = _this$props$location$ === void 0 ? {} : _this$props$location$;
      var _back = _this$props$location$._back;
      var pagination = searchPageData.pagination;
      this.handleSearch(_isUndefined(_back) ? {} : pagination);
    }
    /**
     * 列表查询
     * @param {Object} pagination 查询参数
     */

  }, {
    key: "handleSearch",
    value: function handleSearch() {
      var pagination = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var _this$pageConfig = this.pageConfig;
      _this$pageConfig = _this$pageConfig === void 0 ? {} : _this$pageConfig;
      var searchDispatch = _this$pageConfig.searchDispatch,
          _this$pageConfig$sear = _this$pageConfig.searchCallback,
          searchCallback = _this$pageConfig$sear === void 0 ? function (e) {
        return e;
      } : _this$pageConfig$sear,
          _this$pageConfig$para = _this$pageConfig.paramsFilter,
          paramsFilter = _this$pageConfig$para === void 0 ? function (e) {
        return e;
      } : _this$pageConfig$para,
          _this$pageConfig$othe = _this$pageConfig.otherParams,
          otherParams = _this$pageConfig$othe === void 0 ? {} : _this$pageConfig$othe;
      var _this$props$dispatch = this.props.dispatch,
          dispatch = _this$props$dispatch === void 0 ? function (e) {
        return e;
      } : _this$props$dispatch;
      var form = this.filterForm.props && this.filterForm.props.form;
      var params = _isUndefined(form) ? {} : form.getFieldsValue();
      var filterValues = filterNullValueObject(_objectSpread(_objectSpread({}, params), paramsFilter(params)));
      dispatch({
        type: searchDispatch,
        payload: _objectSpread(_objectSpread({}, otherParams), {}, {
          page: pagination
        }, filterValues)
      }).then(function (res) {
        searchCallback(res);
      });
    }
    /**
     *
     * @param {object} ref - FilterForm子组件对象
     */

  }, {
    key: "handleBindRef",
    value: function handleBindRef() {
      var ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      this.filterForm = ref;
    }
  }, {
    key: "handleRowSelectChange",
    value: function handleRowSelectChange(_, selectedRows) {
      this.setState({
        selectedRows: selectedRows
      });
    }
    /**
     * 查询页面配置，子类覆盖
     */

  }, {
    key: "pageConfig",
    value: function pageConfig() {
      return {};
    }
    /**
     * 自定义初始化数据
     */
    // eslint-disable-next-line

  }, {
    key: "customConstructor",
    value: function customConstructor(props) {
      return {};
    }
    /**
     * 渲染 Header, 子类覆盖
     */

  }, {
    key: "renderHeader",
    value: function renderHeader() {
      return /*#__PURE__*/React.createElement(Header, {
        title: intl.get('cpaas.common.components.searchPage.title').d('默认标题')
      });
    }
    /**
     * Conent 属性, 子类覆盖
     */

  }, {
    key: "contentProps",
    value: function contentProps() {
      return null;
    }
    /**
     * Table 属性, 子类覆盖
     */

  }, {
    key: "tableProps",
    value: function tableProps() {
      return {};
    }
    /**
     * 渲染查询表单, 子类覆盖
     */

  }, {
    key: "renderForm",
    value: function renderForm() {
      return null;
    }
    /**
     * 渲染其他组件，例如弹出框等, 子类覆盖
     */

  }, {
    key: "renderOther",
    value: function renderOther() {
      return null;
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$pageConfig2 = this.pageConfig;
      _this$pageConfig2 = _this$pageConfig2 === void 0 ? {} : _this$pageConfig2;
      var modelName = _this$pageConfig2.modelName,
          _this$pageConfig2$dat = _this$pageConfig2.dataName,
          dataName = _this$pageConfig2$dat === void 0 ? 'list' : _this$pageConfig2$dat,
          _this$pageConfig2$cus = _this$pageConfig2.customSearch,
          customSearch = _this$pageConfig2$cus === void 0 ? false : _this$pageConfig2$cus,
          _this$pageConfig2$edi = _this$pageConfig2.editTable,
          editTable = _this$pageConfig2$edi === void 0 ? false : _this$pageConfig2$edi;
      var _this$state$selectedR = this.state.selectedRows,
          selectedRows = _this$state$selectedR === void 0 ? [] : _this$state$selectedR;
      var _this$props$modelName2 = this.props[modelName],
          searchPageData = _this$props$modelName2 === void 0 ? {} : _this$props$modelName2; // 根据 modelName 获取 Model 数据

      var customTableProps = this.tableProps();
      var _searchPageData$dataN = searchPageData[dataName],
          list = _searchPageData$dataN === void 0 ? {} : _searchPageData$dataN,
          _searchPageData$pagin = searchPageData.pagination,
          pagination = _searchPageData$pagin === void 0 ? {} : _searchPageData$pagin;
      var _customTableProps$row = customTableProps.rowKey,
          rowKey = _customTableProps$row === void 0 ? 'key' : _customTableProps$row;
      var columns = [];
      var rowSelection = {
        selectedRowKeys: selectedRows.map(function (n) {
          return n[rowKey];
        }),
        onChange: this.handleRowSelectChange
      };
      var filterProps = {
        onSearch: this.handleSearch,
        onRef: this.handleBindRef
      };
      var FilterForm = this.FilterForm;

      var tableProps = _objectSpread({
        bordered: true,
        rowKey: 'key',
        dataSource: list.content,
        pagination: pagination,
        rowSelection: rowSelection,
        columns: columns,
        onChange: this.handleSearch
      }, customTableProps);

      return /*#__PURE__*/React.createElement(React.Fragment, null, this.renderHeader(), /*#__PURE__*/React.createElement(Content, this.contentProps(), /*#__PURE__*/React.createElement("div", {
        className: "table-list-search"
      }, /*#__PURE__*/React.createElement(FilterForm, filterProps, function (form) {
        return customSearch ? _this2.renderForm(form) : /*#__PURE__*/React.createElement(Form, {
          layout: "inline"
        }, _this2.renderForm(form), /*#__PURE__*/React.createElement(Form.Item, null, /*#__PURE__*/React.createElement(Button, {
          "data-code": "search",
          type: "primary",
          htmlType: "submit",
          onClick: _this2.filterForm.handleSearch
        }, intl.get('cpaas.common.status.search').d('查询')), /*#__PURE__*/React.createElement(Button, {
          "data-code": "reset",
          style: {
            marginLeft: 8
          },
          onClick: _this2.filterForm.handleFormReset
        }, intl.get('cpaas.common.status.reset').d('重置'))));
      })), editTable ? /*#__PURE__*/React.createElement(EditTable, tableProps) : /*#__PURE__*/React.createElement(CustomTable, tableProps)), this.renderOther());
    }
  }]);

  return SearchPage;
}(Component), (_applyDecoratedDescriptor(_class.prototype, "handleSearch", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "handleSearch"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleBindRef", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "handleBindRef"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleRowSelectChange", [_dec3], Object.getOwnPropertyDescriptor(_class.prototype, "handleRowSelectChange"), _class.prototype)), _class));
export { SearchPage as default };